package com.molichuxing.gateway.bff.car.services.Impl;

import com.molichuxing.framework.dto.Paged;
import com.molichuxing.gateway.bff.car.convertor.Convert;
import com.molichuxing.gateway.bff.car.entity.response.AccidentBizVo;
import com.molichuxing.gateway.bff.car.entity.response.AccidentDetailVo;
import com.molichuxing.gateway.bff.car.services.AccidentBffService;
import com.molichuxing.services.business.dto.response.AccidentBizDto;
import com.molichuxing.services.business.dto.response.AccidentDetailDto;
import com.molichuxing.services.business.service.CarAccidentBizService;
import com.molichuxing.services.infrastructure.dto.response.InsuranceCompanyDto;
import com.molichuxing.services.infrastructure.dto.response.RegionInfoDto;
import com.molichuxing.services.infrastructure.dto.response.UserDto;
import com.molichuxing.services.infrastructure.service.InsuranceCompanyService;
import com.molichuxing.services.infrastructure.service.RegionService;
import com.molichuxing.services.infrastructure.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: renzhengyu
 * @Description:
 * @Date: Created in 14:53 2019/9/11
 * @Modified By:
 **/
@Service("accidentBffService")
public class AccidentBffServiceImpl implements AccidentBffService {

    private static final Logger logger = LoggerFactory.getLogger(AccidentBffServiceImpl.class);

    @Resource
    private CarAccidentBizService carAccidentBizService;

    @Resource
    private UserService userService;

    @Resource
    private InsuranceCompanyService insuranceCompanyService;

    @Resource
    private RegionService regionService;

    @Override
    public Paged<AccidentBizVo> getCarAccidentPage(Map<String, Object> map, Integer pageNum, Integer pageSize) {
        UserDto userDto = null;
        if(map.get("userName") != null) {
            userDto = userService.getUserByRealName(map.get("userName").toString());
            if(null == userDto) {
                map.put("userId", -1);
            }else{
                map.put("userId", userDto.getId());
            }
        }
        Paged<AccidentBizDto> page = carAccidentBizService.getCarAccidentPage(map, pageNum, pageSize);
        if(null == page) {
            return null;
        }
        return Convert.toCarAccidentVoPage(page, getUserMap(page.getList()));
    }

    @Override
    public List<AccidentBizVo> getCarAccidentList(Integer carId) {
        List<AccidentBizDto> result = carAccidentBizService.getAccidentByCarId(carId);
        return Convert.toCarAccidentVoPage(result, getUserMap(result));
    }

    @Override
    public AccidentDetailVo getAccident(Integer accidentId) throws Exception {
        AccidentDetailDto accidentDetailDto = carAccidentBizService.getAccident(accidentId);
        AccidentDetailVo accidentDetailVo =  Convert.toAccidentDetailVo(accidentDetailDto);
        if(null == accidentDetailVo) {
            return null;
        }
        UserDto userDto = null;
        if(null != accidentDetailDto&& null != accidentDetailDto.getAccidentCarUserDto()) {
            Integer userId = accidentDetailDto.getAccidentCarUserDto().getUserId();
            userDto = userService.getUser(userId);
            if(null != userDto) {
                accidentDetailVo.getAccidentCarUserVo().setUserName(userDto.getRealName());
                accidentDetailVo.getAccidentCarUserVo().setUserPhone(userDto.getPhone());
            }
            Integer commercialCompanyId = accidentDetailDto.getAccidentCarUserDto().getCommercialCompanyId();
            InsuranceCompanyDto insuranceCompanyDto = insuranceCompanyService.getInsuranceCompanyName(commercialCompanyId);
            if(null != insuranceCompanyDto) {
                accidentDetailVo.getAccidentCarUserVo().setCommercialCompany(insuranceCompanyDto.getName());
            }
            Integer compulsoryCompanyId = accidentDetailDto.getAccidentCarUserDto().getCompulsoryCompanyId();
            InsuranceCompanyDto compulsoryCompanyDto = insuranceCompanyService.getInsuranceCompanyName(compulsoryCompanyId);
            if(null != compulsoryCompanyDto) {
                accidentDetailVo.getAccidentCarUserVo().setCompulsoryCompany(compulsoryCompanyDto.getName());
            }

        }
        if(null != accidentDetailDto&& null != accidentDetailDto.getAccidentInfoDto()) {
            RegionInfoDto regionInfoDto = regionService.getCityNameByAreaId(accidentDetailDto.getAccidentInfoDto().getRegionId());
            accidentDetailVo.getAccidentInfoVo().setAccidentAddr(regionInfoDto.getProvinceFullName()+regionInfoDto.getCityFullName()+regionInfoDto.getAreaFullName()+accidentDetailDto.getAccidentInfoDto().getAccidentAddr());
        }
        return accidentDetailVo;
    }

    private Map<Integer, UserDto> getUserMap(List<AccidentBizDto> beanList) {
        Map<Integer, UserDto> result = new HashMap<>();
        if((null != beanList)&&(beanList.size() > 0)) {
            List<Integer> userIds = new ArrayList<>();
            for(AccidentBizDto bean : beanList) {
                userIds.add(bean.getUserId());
            }
            result = userService.getUsers(userIds);
        }
        return result;
    }
}
